Begin a transaction, defer a rollback immediately, execute queries with error checking, and commit at the end. The deferred rollback is a no-op if Commit has already been called.
Defer Rollback immediately after BeginTx — ensures rollback on any error path
Pass tx as a DBTX interface to repository functions for unit-of-work reuse
Keep transactions short — long-running transactions hold locks and cause contention
Handle serialization failures (pgcode 40001) with retry logic for high-contention scenarios
Use savepoints for nested transaction-like behavior within a single transaction